﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace HFController
{
    /// <summary>
    /// 法律事务逻辑
    /// </summary>
    public class LegalAffairsController:ControllerBase
    {
        /// <summary>
        /// 本类的单列对象，保证这个类在程序中只存在着一个实例
        /// </summary>
        private static LegalAffairsController _legalAffairsController;

        /// <summary>
        /// 实例化本类（单列模式实例化）
        /// 判断_legalAffairsController对象有没有实例化，实例化了直接返回，没有就实例化
        /// </summary>
        /// <returns></returns>
        public static LegalAffairsController getController()
        {
            if (_legalAffairsController == null)
            {
                _legalAffairsController = new LegalAffairsController();
            }
            return _legalAffairsController;
        }

        #region 法律事务

        /// <summary>
        /// 事务数据库访问
        /// </summary>
        private hf.DAL.LegalAffairs LegalAffairsDAL = new hf.DAL.LegalAffairs();

        /// <summary>
        /// 添加法律事务
        /// </summary>
        /// <param name="LegalAffairsTitle"></param>
        public void doAddLegalAffairs(string LegalAffairsTitle)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.LegalAffairs model = new hf.Model.LegalAffairs();
            model.LegalAffairsTitle = LegalAffairsTitle;

            try
            {
                model.ID = LegalAffairsDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='LegalAffairs.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 修改一条法律事务记录
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="LegalAffairsTitle"></param>
        public void doModifyLegalAffairs(int ID, string LegalAffairsTitle)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            //根据ID，得到一条记录
            hf.Model.LegalAffairs model = this.getLegalAffairsModel(ID);
            //修改这条记录的Title
            model.LegalAffairsTitle = LegalAffairsTitle;

            try
            {
                //执行修改
                LegalAffairsDAL.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='LegalAffairs.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 得到事务列表，用作绑定下拉框（方法名和下面的一样，这个是重载）
        /// </summary>
        /// <param name="LoadCount">读取记录数</param>
        /// <returns></returns>
        public List<hf.Model.LegalAffairs> getLegalAffairsList(int LoadCount)
        {
            List<hf.Model.LegalAffairs> list = new List<hf.Model.LegalAffairs>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = LegalAffairsDAL.GetLegalAffairsList(LoadCount, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 得到事务列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.LegalAffairs> getLegalAffairsList(out string PagerStr)
        {
            List<hf.Model.LegalAffairs> list = new List<hf.Model.LegalAffairs>();

            //每页显示的记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }

            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = LegalAffairsDAL.GetLegalAffairsList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 根据ID得到一条事务记录
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.LegalAffairs getLegalAffairsModel(int ID)
        {
            hf.Model.LegalAffairs model = new hf.Model.LegalAffairs();
            int total;
            List<hf.Model.LegalAffairs> list = LegalAffairsDAL.GetLegalAffairsList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 删除事务
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeLegalAffairs(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                LegalAffairsDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='LegalAffairs.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        } 
        #endregion

        #region 法律事务大分类

        /// <summary>
        /// 法律事务大分类数据访问
        /// </summary>
        private hf.DAL.LegalAffairsBiglClass LegalAffairsBiglClassDAL = new hf.DAL.LegalAffairsBiglClass();

        /// <summary>
        /// 添加法律事务大分类
        /// </summary>
        /// <param name="ClassName"></param>
        /// <param name="LegalAffairsID"></param>
        public void doAddLegalAffairsBiglClass(string ClassName, string LegalAffairsID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.LegalAffairsBiglClass model = new hf.Model.LegalAffairsBiglClass();

            model.ClassName = ClassName;
            model.LegalAffairsID = int.Parse(LegalAffairsID);

            try
            {
                model.ID = LegalAffairsBiglClassDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='LegalAffairsBiglClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 得到事务列表，用作绑定下拉框，增加了参数
        /// </summary>
        /// <param name="LoadCount">读取记录数</param>
        /// <param name="strSql">筛选条件</param>
        /// <returns></returns>
        public List<hf.Model.LegalAffairsBiglClass> getLegalAffairsBiglClass(int LoadCount ,string strSql)
        {
            List<hf.Model.LegalAffairsBiglClass> list = new List<hf.Model.LegalAffairsBiglClass>();
            //总数据记录
            int total;

            //获得列表
            list = LegalAffairsBiglClassDAL.GetLegalAffairsBiglClassList(LoadCount, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 得到法律事务发分类，用于绑定下来框
        /// </summary>
        /// <param name="LoadCount"></param>
        /// <returns></returns>
        public List<hf.Model.LegalAffairsBiglClass> getLegalAffairsBigClass(int LoadCount)
        {
            List<hf.Model.LegalAffairsBiglClass> list = new List<hf.Model.LegalAffairsBiglClass>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = LegalAffairsBiglClassDAL.GetLegalAffairsBiglClassList(LoadCount, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 根据一个ID 得到一条法律事务大分类
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.LegalAffairsBiglClass getLegalAffairsBiglClassModel(int ID)
        {
            hf.Model.LegalAffairsBiglClass model = new hf.Model.LegalAffairsBiglClass();
            int total;

            List<hf.Model.LegalAffairsBiglClass> list = LegalAffairsBiglClassDAL.GetLegalAffairsBiglClassList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }
        /// <summary>
        /// 修改法律事务大分类
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="ClassName"></param>
        /// <param name="LegalAffairsID"></param>
        public void doModifyLegalAffairsBiglClass(int ID, string ClassName, string LegalAffairsID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.LegalAffairsBiglClass model = this.getLegalAffairsBiglClassModel(ID);
            model.ClassName = ClassName;
            model.LegalAffairsID = int.Parse(LegalAffairsID);

            try
            {
                //执行修改
                LegalAffairsBiglClassDAL.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='LegalAffairsBiglClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
        /// <summary>
        /// 得到法律事务大分类列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.LegalAffairsBiglClass> getLegalAffairsBiglClassList(out string PagerStr)
        {
            List<hf.Model.LegalAffairsBiglClass> list = new List<hf.Model.LegalAffairsBiglClass>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = LegalAffairsBiglClassDAL.GetLegalAffairsBiglClassList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 得到法律事务和大类的组合选择框列表
        /// </summary>
        /// <returns></returns>
        public List<hf.Model.LeaglAffairsOption> getOptionList()
        {
            List<hf.Model.LeaglAffairsOption> outList = new List<hf.Model.LeaglAffairsOption>();
            List<hf.Model.LegalAffairs> list = LegalAffairsController.getController().getLegalAffairsList(9999);
            foreach (hf.Model.LegalAffairs l in list)
            {
                List<hf.Model.LegalAffairsBiglClass> listBig = LegalAffairsController.getController().getLegalAffairsBiglClassList(99999, l.ID);
                outList.Add(new hf.Model.LeaglAffairsOption(0 ,l.LegalAffairsTitle));
                foreach (hf.Model.LegalAffairsBiglClass lB in listBig)
                {
                    outList.Add(new hf.Model.LeaglAffairsOption(lB.ID, "&nbsp;&nbsp;|-" + lB.ClassName));
                }
            }
            return outList;
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="IDS"></param>
        /// <returns></returns>
        public List<hf.Model.LegalAffairsBiglClass> getLegalAffairsBiglClassList(int Count, string IDS)
        {
            List<hf.Model.LegalAffairsBiglClass> list = new List<hf.Model.LegalAffairsBiglClass>();

            //没有显示记录
            int PageSize = Count;
            //当前页码
            int PageNumber = 1;
            //记录总条数
            int total;
            //筛选条件
            string strSql = "ID in (" + IDS.ToString() + ")";
            //获得列表
            list = LegalAffairsBiglClassDAL.GetLegalAffairsBiglClassList(PageSize, PageNumber, strSql, out total);

            return list;
        }

        /// <summary>
        /// 得到法律事务大分类列表
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="lID"></param>
        /// <returns></returns>
        public List<hf.Model.LegalAffairsBiglClass> getLegalAffairsBiglClassList(int Count ,int lID)
        {
            List<hf.Model.LegalAffairsBiglClass> list = new List<hf.Model.LegalAffairsBiglClass>();

            //没有显示记录
            int PageSize = Count;
            //当前页码
            int PageNumber = 1;
            //记录总条数
            int total;
            //筛选条件
            string strSql = "LegalAffairsID=" + lID.ToString();

            //获得列表
            list = LegalAffairsBiglClassDAL.GetLegalAffairsBiglClassList(PageSize, PageNumber, strSql, out total);

            return list;
        }

        /// <summary>
        /// 删除法律事务大分类
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeLegalAffairsBiglClass(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                LegalAffairsBiglClassDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='LegalAffairsBiglClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        #endregion

        #region 法律事务小类
        /// <summary>
        /// 法律事务大分类数据访问
        /// </summary>
        private hf.DAL.LegalAffairsSmallClass LegalAffairsSmallClassDAl = new hf.DAL.LegalAffairsSmallClass();

        /// <summary>
        /// 添加法律事务小分类
        /// </summary>
        /// <param name="SmallClassName"></param>
        /// <param name="BigClassName"></param>
        public void doAddLegalAffairsSmallClass(string SmallClassName, string BigClassName)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.LegalAffairsSmallClass model = new hf.Model.LegalAffairsSmallClass();

            model.SmallClassName = SmallClassName;
            model.BigClassName = int.Parse(BigClassName);

            try
            {
                model.ID = LegalAffairsSmallClassDAl.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='LegalAffairsSmallClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
        

        /// <summary>
        /// 得到指定数量小分类列表
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="BigClassID"></param>
        /// <returns></returns>
        public List<hf.Model.LegalAffairsSmallClass> getLegalAffairsSmallClassList(int Count ,int BigClassID)
        {
            List<hf.Model.LegalAffairsSmallClass> list = new List<hf.Model.LegalAffairsSmallClass>();
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "BigClassName=" + BigClassID.ToString();

            //获得列表
            list = LegalAffairsSmallClassDAl.GetLegalAffairsSmallClassList(Count, PageNumber, strSql, out total);

            return list;
        }

        /// <summary>
        /// 得到法律事务小分类列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.LegalAffairsSmallClass> getLegalAffairsSmallClassList(out string PagerStr)
        {
            List<hf.Model.LegalAffairsSmallClass> list = new List<hf.Model.LegalAffairsSmallClass>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = LegalAffairsSmallClassDAl.GetLegalAffairsSmallClassList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 根据一个ID 得到一条法律事务小分类
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.LegalAffairsSmallClass getLegalAffairsSmallClassModel(int ID)
        {
            hf.Model.LegalAffairsSmallClass model = new hf.Model.LegalAffairsSmallClass();
            int total;

            List<hf.Model.LegalAffairsSmallClass> list = LegalAffairsSmallClassDAl.GetLegalAffairsSmallClassList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 得到法律事务小分类，用于绑定下来框
        /// </summary>
        /// <param name="LoadCount"></param>
        /// <returns></returns>
        public List<hf.Model.LegalAffairsSmallClass> getLegalAffairsSmallClass(int LoadCount)
        {
            List<hf.Model.LegalAffairsSmallClass> list = new List<hf.Model.LegalAffairsSmallClass>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = LegalAffairsSmallClassDAl.GetLegalAffairsSmallClassList(LoadCount, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 得到事务大分类列表，用作绑定下拉框，增加了参数
        /// </summary>
        /// <param name="LoadCount">读取记录数</param>
        /// <param name="strSql">筛选条件</param>
        /// <returns></returns>
        public List<hf.Model.LegalAffairsSmallClass> getLegalAffairsSmallClass(int LoadCount, string strSql)
        {
            List<hf.Model.LegalAffairsSmallClass> list = new List<hf.Model.LegalAffairsSmallClass>();
            //总数据记录
            int total;

            //获得列表
            list = LegalAffairsSmallClassDAl.GetLegalAffairsSmallClassList(LoadCount, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 修改法律事务小分类
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="SmallClassName"></param>
        /// <param name="BigClassName"></param>
        public void doModifyLegalAffairsSmallClass(int ID, string SmallClassName, string BigClassName)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.LegalAffairsSmallClass model = this.getLegalAffairsSmallClassModel(ID);
            model.SmallClassName = SmallClassName;
            model.BigClassName = int.Parse(BigClassName);

            try
            {
                //执行修改
                LegalAffairsSmallClassDAl.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='LegalAffairsSmallClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 删除法律事务小分类
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeLegalAffairsSmallClass(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                LegalAffairsSmallClassDAl.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='LegalAffairsSmallClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
        #endregion
    }
}
